/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.businessLayer;


import fri.entiry.FrUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

/**
 *
 * @author NguyenDongUIT
 */
@Stateless
public class UserBean extends GenericDAO<FrUser> implements UserBeanRemote {

    public UserBean() {
        super(FrUser.class);
    }

    public UserBean(Class<FrUser> entityClass) {
        super(entityClass);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public List<FrUser> getAllUser() {
        try {
            Query q = em.createQuery("SELECT c FROM FrUser c");
            List<FrUser> result = (List<FrUser>) q.getResultList();
            return result;

        } catch (Exception e) {

            System.out.println("SERVER : ==>" + e.getMessage());
        }

        return null;
    }

    @Override
    public FrUser getUserByCode(String userCode) {
        try {

            String sql = "select s from FrUser s where s.userCode='" + userCode +"'";
                    
            Query q = em.createQuery(sql);
            List<FrUser> result = (List<FrUser>) q.getResultList();
            if(result!=null)
            {
                FrUser item = result.get(0);
                return item;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    @Override
    public FrUser getUserByUserId(String userId) {
        try {
            
            String sql = "select s from FrUser s where s.userId='" + userId +"'";
                    
            Query q = em.createQuery(sql);
            List<FrUser> result = (List<FrUser>) q.getResultList();
            if(result!=null)
            {
                FrUser item = result.get(0);
                return item;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
    
    

    @Override
    public boolean checkLogin(FrUser user) {
        
        String sql = "select s from FrUser s where (s.email='" + user.getEmail()
                +"' or s.userCode='"+user.getUserCode()
                +"') and s.password='"+user.getPassword()+"'";
                    
        Query q = em.createQuery(sql);
        List<FrUser> result = (List<FrUser>) q.getResultList();
        if(result!=null && result.size()>0)
        {
            return true;
        }
        return false;
    }
    
    

    @Override
    public FrUser getUserByEmail(String email) {
        try {
            
            String sql = "select s from FrUser s where s.email='" + email +"'";
                    
            Query q = em.createQuery(sql);
            List<FrUser> result = (List<FrUser>) q.getResultList();
            if(result!=null)
            {
                FrUser item = result.get(0);
                return item;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    @Override
    public List<FrUser> getUserByFullName(String fullName) {
        try {
            
            
                    
            Query q = em.createQuery("select s from FrUser s where s.fullName = '" + fullName +"'");
            List<FrUser> result = (List<FrUser>) q.getResultList();
            return result;

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    @Override
    public int addUser(FrUser user) {
        try {
            
          return this.save(user);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return 0;
    }

    
    
    
    
    
    
}
